from pratice_threading import blog_spider
import threading
import time


def single_thread():
    print('**************开始执行single_thread***********************')
    for url in blog_spider.urls:
        blog_spider.get_blog_response(url)
    print('**************结束执行single_thread***********************')


def multi_thread():
    print('**************开始执行multi_thread***********************')
    threads = []
    for url in blog_spider.urls:
        threads.append(threading.Thread(target=blog_spider.get_blog_response, args=(url,)))

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()
    print('**************结束执行multi_thread***********************')


# region 反而更慢
def multi_thread_1():
    threads = []
    for url in blog_spider.urls:
        threads.append(threading.Thread(target=blog_spider.get_blog_response, args=(url,)))
    """
        join会等待该线程结束
    """
    for thread in threads:
        thread.start()
        thread.join()
# endregion

if __name__ == '__main__':
    start_time = time.time()
    single_thread()
    end_time = time.time()
    single_during_time = end_time - start_time
    print('sing_thread during:%ss' % single_during_time)

    start_time = time.time()
    multi_thread_1()
    end_time = time.time()
    multi_during_time = end_time - start_time
    print('sing_thread during:%ss' % multi_during_time)
    print('多线程是单线程的%s倍' % (single_during_time/multi_during_time) )
